package easymall.controller;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import easymall.po.User;
import easymall.util.SendEmailUtil;
import easymall.clients.UserService;

@Controller("userController")
@RequestMapping("/user")
public class UserController {
	@Autowired   
	private UserService userService;
	
//	@RequestMapping("/dologin")
//	public String dologin(User user, HttpSession session, Model model) {
//		if(userService.login(user)) {
//			session.setAttribute("user", user);
//			return "main";
//		}else {
//			model.addAttribute("messageError", "用户名或密码错误");
//			return "login";
//		}
//	}
	
	@RequestMapping("/login")
	public String login(User user, HttpSession session, Model model){
		User muser = userService.login(user);
		if(muser != null) {
			session.setAttribute("user", muser);
			return "redirect:/index.jsp";
		}else {
			model.addAttribute("message", "用户名或密码错误！");
			return "login";
		}
	}
	
	@RequestMapping(value = "/checkUser", method = RequestMethod.POST)
	public void check(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String username = request.getParameter("username");
		if(userService.checkUsername(username)) {
			String str = URLEncoder.encode("用户名"+username+"已被注册！", "utf-8");
			response.getWriter().print(str);
		}else {
			String str = URLEncoder.encode("恭喜您，用户名"+username+"可以使用", "utf-8");
			response.getWriter().print(str);
		}
	}
	
	//消息提醒
	@RequestMapping(value="message",produces = "text/html;charset=utf-8")
	@ResponseBody
	public String Message() {
		return "easymall : 账户激活成功！";
	}
	
	//修改userid
	@RequestMapping("motifyvaild")
	public String Motifyvaild(int id) {
		userService.modifyValid(id);
		return "redirect:message";
	}
	
	//尝试登录
	@RequestMapping(value="trylogin")
	public String tryLogin(Integer id, HttpSession session,Model model) {
		int v = userService.getValid(id);
		if(v==1) { //登录成功
			session.setAttribute("user", userService.getUser(id));
			return "redirect:/index.jsp";
		}else { //登陆失败
			model.addAttribute("msg", 1);
			model.addAttribute("id",id);
			return "checkvalid";
		}
	}
	
	
	@RequestMapping("/regist")
	public String regist(User user, String valistr, HttpSession session, Model model) {
		if(user.getUsername() == null || user.getUsername() == "") {
			model.addAttribute("msg", "用户名不能为空");
			return "regist";
		}
		if(user.getPassword() == null || user.getPassword() == "") {
			model.addAttribute("msg", "密码不能为空");
			return "regist";
		}
		if(!valistr.equalsIgnoreCase(session.getAttribute("code").toString())) {
			model.addAttribute("msg", "验证码错误！");
			return "regist";
		}
		if(userService.regist(user)>0) {
//			session.setAttribute("user", user);
//			model.addAttribute("msg", "注册成功");
			try {
//				SendEmailUtil.sendEmail("unixksd1@163.com", 3);
				int id = userService.login(user).getId();
				SendEmailUtil.sendEmail(user.getEmail(), id);
				return "redirect:checkvalid?id="+id; //发送邮件成功
			} catch (Exception e) {
				e.printStackTrace(); //发送邮件失败
				model.addAttribute("msg", "邮件发送失败");
				userService.remove(user); //在数据库中的要删除
				return "regist";
			}
//			model.addAttribute("id", 3);
			
			
		}else {
			model.addAttribute("msg", "注册失败");
			return "regist";
		}
		
		
	}
	
	//等待验证页
	@RequestMapping("checkvalid")
	public String checkvalid(Integer id,Model model) {
		model.addAttribute("id", id);
		return "checkvalid";
	}
}
